{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-06-25T04:49:28.619104Z",
     "start_time": "2025-06-25T04:49:27.919524Z"
    }
   },
   "source": [
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "from langchain_ollama import ChatOllama\n",
    "\n",
    "from jupyter_constants import OLLAMA_DEFAULT_MODEL\n",
    "\n",
    "llm = ChatOllama(\n",
    "    model=OLLAMA_DEFAULT_MODEL,\n",
    ")"
   ],
   "outputs": [],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-25T04:49:28.635628Z",
     "start_time": "2025-06-25T04:49:28.619104Z"
    }
   },
   "cell_type": "code",
   "source": [
    "template = \"\"\"\n",
    "{message}\n",
    "\n",
    "/nothink\n",
    "\"\"\"\n",
    "\n",
    "prompt = ChatPromptTemplate.from_template(template)\n",
    "\n",
    "chat = prompt | llm"
   ],
   "id": "e772c9ad668fc03",
   "outputs": [],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-25T04:49:48.832004Z",
     "start_time": "2025-06-25T04:49:28.647149Z"
    }
   },
   "cell_type": "code",
   "source": [
    "for chuck in chat.stream({\"message\": \"你是谁?\"}):\n",
    "    print(chuck.content, end=\"\", flush=True)"
   ],
   "id": "7442a0de7c3a9e0d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<think>\n",
      "\n",
      "</think>\n",
      "\n",
      "我是通义千问，由通义实验室研发的超大规模语言模型。我能够进行多轮对话，回答各种问题，创作文字，编程，逻辑推理，多语言理解，代码生成等。我旨在为用户提供有帮助，安全，可靠的服务。如果您有任何问题或需要帮助，请随时告诉我！"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-25T04:50:29.409579Z",
     "start_time": "2025-06-25T04:49:48.832004Z"
    }
   },
   "cell_type": "code",
   "source": [
    "for chuck in chat.stream({\"message\": \"用至少三种不同的编程语言实现hello-world。\"}):\n",
    "    print(chuck.content, end=\"\", flush=True)"
   ],
   "id": "3286fb6ff73d1843",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<think>\n",
      "\n",
      "</think>\n",
      "\n",
      "当然可以！以下是使用至少三种不同编程语言实现的 `Hello, World!` 程序：\n",
      "\n",
      "---\n",
      "\n",
      "### 1. **Python**\n",
      "```python\n",
      "print(\"Hello, World!\")\n",
      "```\n",
      "\n",
      "---\n",
      "\n",
      "### 2. **Java**\n",
      "```java\n",
      "public class HelloWorld {\n",
      "    public static void main(String[] args) {\n",
      "        System.out.println(\"Hello, World!\");\n",
      "    }\n",
      "}\n",
      "```\n",
      "\n",
      "---\n",
      "\n",
      "### 3. **JavaScript (Node.js 环境)**\n",
      "```javascript\n",
      "console.log(\"Hello, World!\");\n",
      "```\n",
      "\n",
      "---\n",
      "\n",
      "如果你需要其他语言的实现，比如 C、C++、Ruby、Go 等，也可以告诉我，我可以继续补充！"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-25T04:51:09.628334Z",
     "start_time": "2025-06-25T04:50:29.409579Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from langchain_core.messages import HumanMessage\n",
    "\n",
    "messages = [\n",
    "    HumanMessage(\"一句话解释什么是红烧肉？\"),\n",
    "    HumanMessage(\"一句话解释什么是人工智能。\"),\n",
    "    HumanMessage(\"一句话说明什么是大语言模型。\"),\n",
    "    HumanMessage(\"总结一下呢先前的所有的对话\"),\n",
    "]\n",
    "\n",
    "for chuck in chat.stream(messages):\n",
    "    print(chuck.content, end=\"\", flush=True)"
   ],
   "id": "1a85690ad3ea2d4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<think>\n",
      "\n",
      "</think>\n",
      "\n",
      "红烧肉是一道将猪肉块用酱油、糖等调料炖煮至入味且肉质酥软的中式经典菜肴。\n",
      "\n",
      "人工智能是指由人制造的机器或软件系统，能够执行通常需要人类智能才能完成的复杂任务，如学习、推理、问题解决和感知。\n",
      "\n",
      "大语言模型是一种基于大量文本数据训练而成的深度学习模型，能够理解和生成自然语言，广泛应用于问答、翻译、创作等场景。\n",
      "\n",
      "总结：  \n",
      "之前的对话中，分别解释了红烧肉、人工智能和大语言模型的概念，并最后对所有内容进行了总结。"
     ]
    }
   ],
   "execution_count": 24
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
